/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.springapp.controller;

import com.springapp.model.Animal;
import com.springapp.model.Type;
import com.springapp.service.impl.AnimalServiceImpl;
import com.springapp.service.impl.TypeServiceImpl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Лерок
 */
@Controller
@RequestMapping("/")
public class AnimalAdder {

    @Autowired
    private AnimalServiceImpl asi;

    @Autowired
    private TypeServiceImpl tsi;

    private final Logger log = LoggerFactory.getLogger(AnimalAdder.class);

    @RequestMapping("/")
    public ModelAndView getAnimalCreateForm() {
        List<Animal> animalList = asi.getAll();
        List<Type> typesList = tsi.getAll();
        
     
        log.info("types arr" + typesList);
        log.info("animals arr" + animalList);
        ModelAndView animalForm = new ModelAndView("addAnimal");
        animalForm.addObject("animalsArr", animalList);
        animalForm.addObject("typesArr", typesList);
        return animalForm;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String saveAnimal(String name, int typeId, Double weigth, Integer id) {
        Animal animal = new Animal();
        animal.setName(name);
        animal.setTypeId(typeId);
        if (id != null) {
            animal.setId(id);
        }
        if (weigth != null) {
            animal.setWeigth(weigth.doubleValue());
        }
        asi.save(animal);
        return "redirect:/";
    }

    @RequestMapping(value = "/addType", method = RequestMethod.POST)
    public String createType(String name, Integer id, int[] animalsId) {
        Type type = new Type();
        type.setName(name);

        if (id != null) {
            type.setId(id);
        }
        tsi.create(type);
        for (int i : animalsId) {
            Animal a = asi.getById(i);
            a.setTypeId(type.getId());
            asi.save(a);
        }
        return "redirect:/";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String deleteAnimal(Integer id) {
        asi.delete(id);
        return "redirect:/";
    }

    @RequestMapping(value = "/deleteType", method = RequestMethod.POST)
    public String deleteType(Integer id) {
        tsi.delete(id);
        return "redirect:/";
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public ModelAndView findType(String name) {
        log.info("im here, sheet!");
        List<Type> allTypes = tsi.getAll();
        log.info("types arr" + allTypes);
        String typeName = null;
        for (int i = 0; i < allTypes.size(); i++) {
            if ((allTypes.get(i).getName()).equals(name)) {
                typeName = allTypes.get(i).getName();
            }
        }
        ModelAndView mav = new ModelAndView("showTypes");
        mav.addObject("type", typeName);
        return mav;
    }
}
